package com.jn.okhttp.adapter;

import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jn.ok_http_demo.R;
import com.jn.okhttp.entity.HotSearchEntity;
import com.jn.okhttp.entity.MusicEntity;

import java.util.List;

public class HotSearchAdapter extends RecyclerView.Adapter<HotSearchAdapter.HotSearchViewHolder> {

    private List<HotSearchEntity.HotItem> mlist;

    public HotSearchAdapter(List<HotSearchEntity.HotItem> items) {
        mlist = items;
    }
    public void updateData(List<HotSearchEntity.HotItem> list) {
        mlist.clear();
        mlist = list;
        notifyDataSetChanged();
    }
    @Override
    public HotSearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hot_search, parent, false);
        return new HotSearchViewHolder(view);
    }

    @Override
    public void onBindViewHolder(HotSearchViewHolder holder, int position) {
        HotSearchEntity.HotItem item = mlist.get(position);
        holder.title.setText(item.getTitle());
        holder.index.setText(String.valueOf(item.getIndex()));
        holder.hotValue.setText(item.getHotValue());

        holder.itemView.setOnClickListener(v -> {
            // 打开链接
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getLink()));
            v.getContext().startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return mlist.size();
    }

    static class HotSearchViewHolder extends RecyclerView.ViewHolder {
        TextView title, index, hotValue;

        HotSearchViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            index = itemView.findViewById(R.id.index);
            hotValue = itemView.findViewById(R.id.hot_value);
        }
    }
}